package de.yellowshoes.tedx.net;

import java.util.ArrayList;
import java.util.List;

import android.util.Log;
import de.yellowshoes.tedx.TEDxConstants;
import de.yellowshoes.tedx.net.exception.CsvReaderException;
import de.yellowshoes.tedx.persistence.entity.Organizer;

/**
 * Reads speaker infos from a csv file.
 * 
 * @author calibanatspace
 * 
 */
public class OrganizerReader
    {
    private String url = null;

    public OrganizerReader( String url )
        {
        this.url = url;
        }

    public List< Organizer > read()
        {
        CsvRemoteReader reader = new CsvRemoteReader( url, ";", 9 );
        List< Object[] > objectList = null;
        List< Organizer > organizerList = new ArrayList< Organizer >();

        try
            {
            objectList = reader.read();
            }
        catch( CsvReaderException e )
            {
            Log.e( TEDxConstants.LOG_TAG, "Error while reading speakers.csv via url " + url, e );
            return null;
            }

        if( objectList.size() > 0 )
            {
            objectList.remove( 0 );
            }

        for( Object[] objects : objectList )
            {
            if( objects.length > 3 )
                {
                Organizer organizer = new Organizer();
                organizer.setDisplayname( ( String )objects[ 0 ] );
                organizer.setDescription( ( String )objects[ 1 ] );
                organizer.setImageUrl( ( String )objects[ 2 ] );
                organizer.setTwitterUrl( ( String )objects[ 3 ] );
                organizer.setFacebookUrl( ( String )objects[ 4 ] );
                organizer.setXingUrl( ( String )objects[ 5 ] );
                organizer.setLinkedinUrl( ( String )objects[ 6 ] );
                organizer.setGoogleUrl( ( String )objects[ 7 ] );
                organizer.setOrder( objects[ 8 ] != null ? Integer.parseInt( ( String )objects[ 8 ] ) : null );

                organizerList.add( organizer );
                }
            }

        return organizerList;
        }
    }
